#!/usr/bin/env python

# Copyright (c) 2009, Purdue University
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice, this
# list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
# 
# Neither the name of the Purdue University nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""This script is used for verifying zone files are correctly loaded on a 
name server."""

__copyright__ = 'Copyright (C) 2009, Purdue University'
__license__ = 'BSD'
__version__ = '0.18'

import os
import sys

from optparse import OptionParser
from roster_core import constants
from roster_config_manager import config_lib
from roster_config_manager import dns_query_lib

def main(args):
  usage = ('\n'
           '\n'
           'To test a DNS server:\n'
           '\t%s -c <config-file> -i <audit-log-id> -s <dns-server> '
           '(-z <zone-name>) (-v <view-name>) or\n'
           '\t%s -f <zone-file> '
           '-s <dns-server>\n') % (sys.argv[0], sys.argv[0])

  parser = OptionParser(version='%%prog (Roster %s)' % __version__, usage=usage)

  parser.add_option('--export-config', action='store_true', dest='export_config',
                    help='This flag is used when dnsquerycheck is called from '
                    'dnsexportconfig. This should not be used by any user.',
                    metavar='<export_config>', default=False)
  parser.add_option('-c', '--config-file', action='store', dest='config_file',
                    help='Roster Server config file', metavar='<config_file>',
                    default=constants.SERVER_CONFIG_FILE_LOCATION)
  parser.add_option('-i', '--id', action='store', dest='id',
                    help='Audit log ID', metavar='<id>',
                    default=None)
  parser.add_option('-d', '--dns-server', action='store', dest='server',
                    help='DNS server to query.', metavar='<server>',
                    default=None)
  parser.add_option('-p', '--port', action='store', dest='port',
                    help='Port to query DNS server on.', metavar='<port>',
                    default=53)
  parser.add_option('-n', '--number', action='store', dest='number',
                    type='int', help='Number of random records to query for. '
                                     'Default=5\n'
                                     'To query all records, use -n 0',
                    metavar='<number>', default=5)
  parser.add_option('-f', '--file', action='store', dest='zone_file',
                    help='Zone file to use for queries, instead of audit '
                    'log id.', metavar='<zone_file>', default=None)
  parser.add_option('-v', '--view', action='store', dest='view_name',
                    help='Check only a specific view. (optional)',
                    metavar='<view_name>', default=None)
  parser.add_option('-z', '--zone', action='store', dest='zone_name',
                    help='Check only a specific zone. (optional)',
                    metavar='<zone_name>', default=None)

  (globals()["options"], args) = parser.parse_args(args)

  if( options.server is None ):
    print 'Must specify -d/--dns-server flag.'
    sys.exit(1)

  if( (options.config_file is None or options.id is None) and 
       options.zone_file is None ):
    print 'Must specify -i/--id and -c/--config-file or the -f/--file flag.'
    sys.exit(1)

  bad_zones = []
  #If we're querying from id and config file
  if( not options.zone_file ):
    config_lib_instance = config_lib.ConfigLib(options.config_file)
    if( not options.export_config ):
      config_lib_instance.UnTarDnsTree(options.id)
    server_dir = os.path.join(config_lib_instance.root_config_dir,
                              options.server)

    view_list = config_lib_instance.GetZoneList(options.server)
  
    if( options.view_name is not None ):
      if( options.view_name not in view_list ):
        print 'View %s not found' % options.view_name
        sys.exit(1)
      else:
        view_list = {options.view_name: view_list[options.view_name]}

    for view_name in view_list:
      zone_list = view_list[view_name]

      if( options.zone_name is not None ):
        if( options.zone_name not in zone_list ):
          print 'Zone %s not found' % options.zone_name
          sys.exit(1)
        else:
          zone_list = {options.zone_name: zone_list[options.zone_name]}

      for zone_name in zone_list:
        zone_file_name = zone_list[zone_name]

        zone_file = os.path.join(server_dir, 'named', view_name, zone_file_name)
        try:
          result = dns_query_lib.QueryFromZoneFile(zone_file, options.server,
              options.port, options.number, view_name=view_name)
        except (config_lib.QueryCheckError, config_lib.ConfigManagerError) as e:
          print 'ERROR: %s' % e.message
          sys.exit(1)
        if( not result ):
          bad_zones.append(zone_file_name.split('/').pop().split('.')[0])
  else:
    try:
      result = dns_query_lib.QueryFromZoneFile(options.zone_file, options.server,
          options.port, options.number)
    except (config_lib.QueryCheckError, config_lib.ConfigManagerError) as e:
      print 'ERROR: %s' % e.message
      sys.exit(1)
    if( not result ):
      bad_zones.append(options.zone_file.split('/').pop().split('.')[0])

  if( len(bad_zones) > 0 ):
    for zone in bad_zones:
      print 'Zone %s does not appear to be online.' % zone
    sys.exit(1)


if( __name__ == "__main__" ):
  main(sys.argv[1:])
  
